home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / pcrng.zip / RMUNI.ASM < prev    next >
Assembly Source File  |  1990-06-29  |  15KB  |  367 lines

  1.               .8087
  2.               page ,132
  3. ;
  4. ;***********************************************************************
  5. ;* Here are data declarations for use by RINIT, IVNI, IUNI, VNI, UNI.  *
  6. ;* All routines are written for use with IBM Fortran/2 Compiler.       *
  7. ;*                                                                     *
  8. ;*   Authors: G. Marsaglia, B. Narasimhan and Arif Zaman               *
  9. ;*                 Supercomputer Computations Research Institute       *
  10. ;*                         and                                         *
  11. ;*                 Department of Statistics                            *
  12. ;*                 Florida State University                            *
  13. ;*                 Tallahassee, Fl 32306-3033.                         *
  14. ;***********************************************************************
  15. ;
  16.               .MODEL     small                        ;Use small memory model
  17.               TITLE      'A Random Number Generator for PC's.'
  18. ;
  19. ;---------------Equates-----------------
  20. ;
  21. arg_offset    equ            6                        ;Offset of argument.
  22. c_low         equ       0cbb1h
  23. c_high        equ          74h                        ;C = 7654321 initially.
  24. cons_low      equ        55e5h
  25. cons_high     equ        159ah                        ;Constant = 362436069.
  26. no_of_bits    equ           32                        
  27. top_of_list   equ          168
  28. init_i_val    equ  top_of_list
  29. init_j_val    equ           84
  30. ;
  31. datseg        segment     para 'F@DATA'
  32. ;
  33. ; Table for Generator (Default values).
  34. ;
  35. unitabl       dd         0F83CEE7Bh
  36.               dd         0A83E5AD3h
  37.               dd          036200BBh
  38.               dd         0FA5764F6h
  39.               dd         0A13CBFC4h
  40.               dd          565A191Eh
  41.               dd          14D4CCFBh
  42.               dd          7F5AD22Ch
  43.               dd          03528F2Eh
  44.               dd         0E81E32DDh
  45.               dd          71C47276h
  46.               dd         0AA0F8045h
  47.               dd          3C3F1C78h
  48.               dd         0E8CE101Dh
  49.               dd         0CCC12691h
  50.               dd          47196DBFh
  51.               dd          074A6DFFh
  52.               dd          03FB675Eh
  53.               dd          60436236h
  54.               dd          072F0247h
  55.               dd          0C7E9185h
  56.               dd         0CE579EAEh
  57.               dd          01864E96h
  58.               dd         0A526C5C6h
  59.               dd         0F582EB0Dh
  60.               dd         0AFE827F4h
  61.               dd          55CED836h
  62.               dd          26124C49h
  63.               dd          7049AEE1h
  64.               dd          49552795h
  65.               dd         0D1602ED6h
  66.               dd          051C65CEh
  67.               dd         0AEF3CC37h
  68.               dd         0A83880ABh
  69.               dd         0AE7EE06Ah
  70.               dd         0D64D988Bh
  71.               dd          0A96BC73h
  72.               dd         0ECEF4297h
  73.               dd          6C18300Dh
  74.               dd          22F3A897h
  75.               dd         0B4D760ADh
  76.               dd         0AC838383h
  77.               dd         0FD04E68Fh
  78. ;
  79. i             dw               164
  80. j             dw                80                    ;Indices I and J.
  81. c             dd         0EADA75CCh                   ;Constant C.
  82. mod169        db               169                  
  83. mod179        db               179                  
  84. multiplier    db                53
  85. carry         db                 1                    ;Carry bit (Default).
  86. stored_val    dd          122a70c3h
  87. datseg        ends
  88. ;
  89. ;***********************************************************************
  90. ;* Subroutine RINIT takes an 8-digit number as argument. Use as        *
  91. ;*                  CALL RINIT(I)                                      *
  92. ;*                                                                     *
  93. ;*  Purpose: Seeds the table and prepares pointers for use by other    *
  94. ;*           routines.                                                 *
  95. ;*                                                                     *
  96. ;* This is set up for use with IBM Fortran/2 Compiler.                 *
  97. ;* -----Note that a 8087 co-processor is assumed to be present.------- *
  98. ;*                                                                     *
  99. ;*   Authors: G. Marsaglia, B. Narasimhan and A. Zaman                 *
  100. ;*            Supercomputer Computations Research Institute            *
  101. ;*                    and                                              *
  102. ;*            Department of Statistics                                 *
  103. ;*            Florida State University                                 *
  104. ;*            Tallahassee, Fl 32306-3033.                              *
  105. ;***********************************************************************
  106. ;
  107. rseg          segment    'CODE'
  108. ;
  109.               assume cs:rseg, ds:datseg
  110. rinit         proc       Far
  111.               public     rinit
  112. ;
  113. ; Usual IBM Fortran/2 stuff....
  114. ;
  115.               push       bp                           ;Save caller's BP.
  116.               mov        bp,sp                        ;Ready to address.
  117. ;
  118. ; No traceback needed.
  119. ;
  120.               mov        ax,datseg                    ;DATA segment.
  121.               mov        ds,ax
  122. ;
  123. ; Load parameter and strip two digits at a time to get four seed values.
  124. ;
  125.               les        si,DWORD PTR[bp].arg_offset
  126.               mov        ax,Word Ptr es:[si]
  127.               mov        dx,Word Ptr es:[si]+2
  128.               mov        cx,10000
  129.               div        cx 
  130.               mov        cl,100
  131.               div        cl
  132.               mov        bx,ax                        ;BH = K, BL = L.
  133.               mov        ax,dx
  134.               div        cl                    
  135.               mov        dl,ah                
  136.               mov        dh,al                        ;DH = J, DL = I.        
  137. ;
  138. ; Add one to all to make sure they are non-zero.
  139. ;
  140.               inc        dl
  141.               inc        dh
  142.               inc        bh
  143.               inc        bl
  144. ;
  145. ; DL = I, DH = J, BL = L, BH = K.
  146. ;
  147.               xor        si,si                        ;Table Index.
  148. labl01:
  149. ;
  150. ; Result will be in [BP,DI].
  151. ;
  152.               xor        di,di                        
  153.               xor        bp,bp
  154.               mov        cx,no_of_bits                ;Bit counter.
  155. labl02:
  156.               shl        di,1              
  157.               rcl        bp,1                         ;Result *= 2.
  158.               mov        al,dh                        ;AL = J.
  159.               mul        bh                           ;AX = J * K.
  160.               div        mod179
  161.               mov        al,ah                        ;AL = AX mod 179.
  162.               mul        dl                           ;AX = AX * I.
  163.               div        mod179
  164.               mov        dl,dh                        ;I = J.
  165.               mov        dh,bh                        ;J = K.
  166.               mov        bh,ah                        ;K = new K.
  167. ;
  168.               mov        al,bl                        ;AL = L.
  169.               mul        multiplier
  170.               inc        ax
  171.               div        mod169
  172.               mov        bl,ah                        ;L = 53*L+1 mod 169.
  173. ;
  174.               mov        al,bl                        ;AL = new L.
  175.               mul        bh                           ;AX = L * K.
  176.               and        ax,63                        ;Mod 64.
  177.               cmp        ax,32
  178.               jl         labl03
  179.               inc        di                           ;Set low bit.
  180. labl03:
  181.               loop       labl02          
  182. ;
  183.               mov        WORD PTR unitabl[si],di       
  184.               mov        WORD PTR unitabl[si+2],bp    ;Save value.
  185.               add        si,4                         ;Bump pointer.
  186.               cmp        si,172                       ;Seeded 86 entries?
  187.               jne        labl01
  188. ;
  189. ; Set indices.
  190. ;
  191.               mov        i,init_i_val
  192.               mov        j,init_j_val
  193. ;
  194. ; Set carry to zero.
  195. ;
  196.               mov        carry,0
  197. ;
  198. ; Set constants.
  199. ;
  200.               mov        WORD PTR c,c_low
  201.               mov        WORD PTR c+2,c_high
  202. ;
  203. ; Ready the first value.
  204. ;
  205.               mov        ax,offset stored_val
  206.               mov        bx,seg stored_val
  207.               push       bx
  208.               push       ax
  209.               call       Far Ptr ivni
  210. ;        FINIT                                        ;Clear 8087.
  211.               pop        ax
  212.               pop        ax
  213. ;
  214. ; Epilogue...
  215.               pop        bp
  216.               ret        4
  217. rinit         endp
  218. ;
  219. ;***********************************************************************
  220. ;* This proc contains the following functions.                         *
  221. ;*                                                                     *
  222. ;*  IVNI     : A function that returns a signed random integer between *
  223. ;*             -2**31 and 2**31-1.                                     *
  224. ;*  IUNI     : A function that returns a positive random integer       *
  225. ;*             between 0 and 2**31-1.                                  *
  226. ;*  VNI      : A function that returns a signed random uniform between *
  227. ;*             -1 and 1.                                               *
  228. ;*  UNI      : A function that returns a positive random uniform       *
  229. ;*             between 0 and 1.                                        *
  230. ;*                                                                     *
  231. ;*  All routines mix a subtract-with-borrow generator and an           *
  232. ;*  arithmetic sequence:                                                          *
  233. ;*              x(n) = x(n-22) - x(n-43) - carry mod 2**32-5.          *
  234. ;*              c = c + 362436069 mod 2**32.                           *
  235. ;*              result = x(n) - c mod 2**32.                           *
  236. ;*                                                                     *
  237. ;* This is set up for use with IBM Fortran/2 Compiler.                 *
  238. ;* -----Note that a 8087 co-processor is assumed to be present.------- *
  239. ;*                                                                     *
  240. ;*   Authors: G. Marsaglia, B. Narasimhan and A. Zaman                 *
  241. ;*            Supercomputer Computations Research Institute            *
  242. ;*                    and                                              *
  243. ;*            Department of Statistics                                 *
  244. ;*            Florida State University                                 *
  245. ;*            Tallahassee, Fl 32306-3033.                              *
  246. ;***********************************************************************
  247. ;
  248.               assume cs:rseg, ds:datseg
  249. ivni          proc       Far                          ;First entry point.
  250.               public     ivni
  251.               mov        ax,datseg                 
  252.               mov        ds,ax                     
  253.               jmp        ready_next_and_save
  254. ;
  255. iuni          label      Far                          ;Second entry point.
  256.               public     iuni
  257.               mov        ax,datseg                 
  258.               mov        ds,ax
  259.               and        Word Ptr stored_val+2,7fffh  ;Mask sign bit.
  260.               jmp        ready_next_and_save
  261. ;
  262. vni           label      Far                          ;Third entry point.
  263.               public     vni
  264.               mov        ax,datseg                 
  265.               mov        ds,ax                     
  266.               mov        ax,Word Ptr stored_val
  267.               mov        bx,Word Ptr stored_val+2
  268.               mov        di,ax                        ;Sign is last bit.
  269.               jmp        normalize
  270. ;
  271. uni           label      Far                          ;Fourth entry point.
  272.               public     uni
  273.               mov        ax,datseg                 
  274.               mov        ds,ax                     
  275.               mov        ax,Word Ptr stored_val
  276.               mov        bx,Word Ptr stored_val+2
  277.               xor        di,di                        ;Sign should be zero.
  278. ;
  279. ; Normalize the number.
  280. ;
  281. normalize:
  282.               mov        cx,no_of_bits                ;Bit counter.
  283. labl04:
  284.               shl        ax,1                         ; Shift left.
  285.               rcl        bx,1
  286.               jc         labl05
  287.               loop       labl04
  288.               jmp        ready_next_and_save            ;Store zero.
  289. labl05:
  290.               add        cx,94
  291.               mov        al,ah
  292.               mov        ah,bl
  293.               mov        bl,bh
  294.               mov        bh,cl
  295.               test       di,1
  296.               jz         labl06
  297.               stc
  298. labl06:
  299.               rcr        bx,1
  300.               rcr        ax,1
  301.               mov        Word Ptr stored_val, ax
  302.               mov        Word Ptr stored_val+2,bx
  303. ;
  304.         FLD              DWord Ptr stored_val         ;Comply with IBM.
  305. ;
  306. ready_next_and_save:
  307.               mov        di,j                         ;Load index j.
  308.               mov        cx,Word Ptr unitabl[di]
  309.               mov        dx,Word Ptr unitabl[di]+2    ;U(j) in DX, CX.
  310. ;
  311.               sub        di,4                         ;j <- j - 4.
  312.               jns        update_j
  313.               mov        di,168
  314. update_j: 
  315.               mov        j,di
  316. ;
  317.               mov        si,i                         ;Load index i.
  318.               mov        ah,carry                     ;Load carry.
  319.               sahf
  320.               sbb        cx,Word Ptr unitabl[si]
  321.               sbb        dx,Word Ptr unitabl[si]+2    ;U(j)-U(i) in DX, CX.
  322.               lahf
  323.               mov        carry,ah                     ;Save carry.
  324.               jnc        dont_modify
  325. ;
  326. ;Negative.
  327. ;
  328.               sub        cx,5                         ;Subtract 5.
  329.               sbb        dx,0
  330. dont_modify:
  331. ;
  332. ;Save result.
  333. ;
  334.               mov        Word Ptr unitabl[si],cx
  335.               mov        Word Ptr unitabl[si]+2,dx
  336. ;
  337.               sub        si,4                         ;i <- i - 4.
  338.               jns        update_i
  339.               mov        si,168
  340. update_i:
  341.               mov        i,si
  342.  
  343. ;
  344. ;Prepare arithmetic sequence.
  345. ;
  346.               mov        ax,Word Ptr c
  347.               mov        bx,Word Ptr c+2
  348.               sub        ax,cons_low
  349.               sbb        bx,cons_high
  350.               mov        Word Ptr c,ax
  351.               mov        Word Ptr c+2,bx
  352. ;
  353.               sub        cx,ax
  354.               sbb        dx,bx
  355.            mov        ax,Word Ptr stored_val
  356.               mov        Word Ptr stored_val,cx
  357.               mov        cx,Word Ptr stored_val+2      
  358.               mov        Word Ptr stored_val+2,dx     ;Save for next time.
  359.               mov        dx,cx                        ;Comply with IBM
  360.                                                       ; integer*4.
  361.               ret
  362. ivni          endp
  363. ;
  364. rseg          ends
  365.               END
  366.